package system

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

// 管理员表
type SysUser struct {
	SysUserId     uint64    `orm:"column(sys_user_id);pk"`
	SysUserName   string    `description:"账号名"`
	SysUserAvatar string    `description:"用户头像"`
	RealName      string    `description:"真实姓名"`
	Password      string    `json:"-" description:"密码"`
	Enabled       int8      `description:"1:有效，0:禁用"`
	Email         string    `description:"邮箱"`
	LastLoginTime time.Time `description:"最近登陆时间"`
	Mobile        string    `description:"手机号"`
	DeptId        int64     `description:"部门id"`
	DisableTime   time.Time `description:"禁用时间"`
	RoleId        int64     `description:"角色ID"`
	Position      string    `description:"职位"`
	Remark        string    `description:"备注"`
	Token         string    `json:"-" description:"token"`
	CampusId      int       `description:"校区id"`
	CampusName    string    `description:"校区名称"`
	ClassId       int       `description:"班级ID"`
	ClassName     string    `description:"班级名称"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	ScenicSpotId  string    `description:"景区id,多个用英文逗号隔开"`
}

// Add 新增
func (a *SysUser) Add() (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(a)
	return
}

type SysUserAddReq struct {
	SysUserName  string `description:"账号名"`
	RealName     string `description:"真实姓名"`
	Password     string `description:"密码"`
	Enabled      int8   `description:"1:有效，0:禁用"`
	Email        string `description:"邮箱"`
	Mobile       string `description:"手机号"`
	DeptId       int64  `description:"部门id"`
	RoleId       int64  `description:"角色ID"`
	Position     string `description:"职位"`
	Remark       string `description:"备注"`
	CampusId     int    `description:"校区id"`
	CampusName   string `description:"校区名称"`
	ClassId      int    `description:"班级ID"`
	ClassName    string `description:"班级名称"`
	ScenicSpotId string `description:"景区id,多个用英文逗号隔开"`
}

type SysUserEditReq struct {
	SysUserId    uint64 `description:"用户id"`
	SysUserName  string `description:"账号名"`
	RealName     string `description:"真实姓名"`
	Enabled      int8   `description:"账号状态：1:有效，0:禁用"`
	Email        string `description:"邮箱"`
	Mobile       string `description:"手机号"`
	DeptId       int64  `description:"部门id"`
	RoleId       int64  `description:"角色ID"`
	Position     string `description:"职位"`
	Remark       string `description:"备注"`
	CampusId     int    `description:"校区id"`
	CampusName   string `description:"校区名称"`
	ClassId      int    `description:"班级ID"`
	ClassName    string `description:"班级名称"`
	ScenicSpotId string `description:"景区id,多个用英文逗号隔开"`
}

func (a *SysUser) GetSysUserByUserName(sysUserName string) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE sys_user_name=? `
	err = o.Raw(sql, sysUserName).QueryRow(&item)
	return
}

func (a *SysUser) GetSysUserByRealName(realName string) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE real_name=? `
	err = o.Raw(sql, realName).QueryRow(&item)
	return
}

func (a *SysUser) GetSysUserByMobile(mobile string) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE mobile=? `
	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// 修改
func (a *SysUser) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrm()
	ptrStructOrTableName := "sys_user"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return

}

func (r *SysUser) GetSysUserPageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM sys_user AS a WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *SysUser) GetSysUserPage(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUserListTmpItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.*,b.role_name FROM sys_user AS a
			LEFT JOIN sys_role AS b ON a.role_id=b.role_id
            WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type SysUserListReq struct {
	DeptId  int64  `description:"部门id"`
	KeyWord string `description:"关键词"`
	Enabled int    `description:"-1全部，1:有效，0:禁用"`
	utils.PagingReq
}

type SysUserPageResp struct {
	List   []*SysUserListItem
	Paging *utils.PagingResp
}

type SysUserListTmpItem struct {
	SysUserId    uint64    `description:"账号id"`
	SysUserName  string    `description:"账号名"`
	RealName     string    `description:"真实姓名"`
	Enabled      int8      `description:"1:有效，0:禁用"`
	Email        string    `description:"邮箱"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"最后更新时间"`
	Mobile       string    `description:"手机号"`
	DeptId       int64     `description:"部门id"`
	RoleId       int64     `description:"角色ID"`
	RoleName     string    `description:"角色名称"`
	DeptFullName string    `description:"部门全称"`
	Position     string    `description:"职位"`
	Remark       string    `description:"备注"`
	ScenicSpotId string    `description:"景区id"`
}

type SysUserListItem struct {
	SysUserId    uint64 `description:"账号id"`
	SysUserName  string `description:"账号名"`
	RealName     string `description:"真实姓名"`
	Enabled      int8   `description:"1:有效，0:禁用"`
	Email        string `description:"邮箱"`
	Mobile       string `description:"手机号"`
	DeptId       int64  `description:"部门id"`
	RoleId       int64  `description:"角色ID"`
	RoleName     string `description:"角色名称"`
	DeptFullName string `description:"部门全称"`
	CreateTime   string `description:"创建时间"`
	ModifyTime   string `description:"最后更新时间"`
	Position     string `description:"职位"`
	Remark       string `description:"备注"`
	ScenicSpotId string `description:"景区id"`
}

// GetSysUserBySysUserId 根据adminId获取用户信息
func (a *SysUser) GetSysUserBySysUserId(sysUserId uint64) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE sys_user_id=? `
	err = o.Raw(sql, sysUserId).QueryRow(&item)
	return
}

type ModifyPwdReq struct {
	SysUserId  uint64 `description:"账号ID"`
	Pwd        string `description:"新密码"`
	ConfirmPwd string `description:"确认新密码"`
}

type ModifyMyPwdReq struct {
	OldPwd     string `description:"原密码"`
	NewPwd     string `description:"新密码"`
	ConfirmPwd string `description:"新密码确认"`
}

type ModifyMyInitPwdReq struct {
	NewPwd     string `description:"新密码"`
	ConfirmPwd string `description:"新密码确认"`
}

type SysAdminReq struct {
	SysUserId uint64 `description:"账号ID"`
}

type SysAdminModifyEnabledReq struct {
	SysUserId uint64 `description:"账号ID"`
	Enabled   int8   `description:"1:有效，0:禁用"`
}

// 删除
func (a *SysUser) Delete() (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM sys_user WHERE sys_user_id=? `
	_, err = o.Raw(sql, a.SysUserId).Exec()
	return
}

func (a *SysUser) GetAdminListByCondition(condition string, pars []interface{}) (list []*SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE 1=1 `
	sql += condition
	sql += ` ORDER BY create_time DESC `
	_, err = o.Raw(sql, pars...).QueryRows(&list)
	return
}

func (a *SysUser) GetSysUserBySysUserName(sysUserName string) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE sys_user_name=? `
	err = o.Raw(sql, sysUserName).QueryRow(&item)
	return
}

func GetSysUserByToken(token string) (item *SysUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_user WHERE token=? `
	err = o.Raw(sql, token).QueryRow(&item)
	return
}
